package com.raincat.multimediapicker;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import java.util.List;

/**
 * @author 躲雨的猫
 */
class FragmentTabAdapter {
    private List<Fragment> fragments; // 一个tab页面对应一个Fragment
    private FragmentManager fragmentManager;
    private int fragmentContentId; // Activity中所要被替换的区域的id

    private int currentTab = 0; // 当前Tab页面索引

    FragmentTabAdapter(FragmentActivity appCompatActivity, List<Fragment> fragments, int fragmentContentId) {
        this.fragments = fragments;
        this.fragmentContentId = fragmentContentId;
        fragmentManager = appCompatActivity.getSupportFragmentManager();

        Fragment fragment = fragmentManager.findFragmentByTag(0 + "");
        if (fragment == null) {
            FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.add(fragmentContentId, fragments.get(0), "0");
            ft.commit();
        }
    }

    /**
     * 添加 fragment
     */
    void addFragment(Fragment fragment, Bundle bundle) {
        fragments.add(fragment);
        if (bundle != null)
            fragment.setArguments(bundle);
        FragmentTransaction ft = obtainFragmentTransaction();
        getCurrentFragment().onPause();
        if (fragment.isAdded()) {
            fragment.onResume(); // 启动目标tab的onResume()
        } else {
            ft.add(fragmentContentId, fragment, fragments.size() - 1 + "");
        }
        showTab(fragments.size() - 1); // 显示目标tab
        ft.commit();
    }

    boolean removeFragment() {
        if (currentTab != 0) {
            FragmentTransaction ft = obtainFragmentTransaction();
            ft.remove(fragments.get(currentTab));
            fragments.remove(currentTab);
            ft.commit();
            currentTab--;
            ft.show(getCurrentFragment());
            getCurrentFragment().onResume();
            return false;
        } else
            return true;
    }

    /**
     * 切换tab
     */
    private void showTab(int idx) {
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragmentManager.findFragmentByTag(i + "");
            if (fragment == null)
                fragment = fragments.get(i);
            FragmentTransaction ft = obtainFragmentTransaction();

            if (idx == i) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commit();
        }
        currentTab = idx; // 更新目标tab为当前tab
    }

    /**
     * 获取一个带动画的FragmentTransaction
     */
    private FragmentTransaction obtainFragmentTransaction() {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setCustomAnimations(R.anim.fade_ins, R.anim.fade_outs);
        return ft;
    }

    private Fragment getCurrentFragment() {
        return fragments.get(currentTab);
    }
}
